from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.shortcuts import redirect
import requests
# 正则表达式的模块
import re

# Create your views here.
def home(request):
    # 去request中可以获取请求的数据
    return HttpResponse("views成功")
def do_home(request):
    data = "紫嫣"
    lst = ["嫣然", "萌萌", "婉云"]
    d = {
        "name":"一萌",
        "sex":"女",
        "age":18
    }
    content = {
        "v1":data,
        "v2":lst,
        "v3":d
    }
    # 去request中可以获取请求的数据
    # django寻找html模板的顺序
    # 1 配置setting.py中的TEMPLATES的DIRS属性
    # return render(request, "do_home.html", content)
    # 2 去找已注册的所有app里面的templates目录中寻找
    # 会优先去外面的setting.py中的TEMPLATES的DIRS查找，如果这里没有就会来app这里查找
    return render(request, "app_home.html", content)
# 获取天气
def do_tianqi(request):
    url = "http://www.weather.com.cn/weather1d/101010100.shtml"
    # 打开浏览器并打开网址
    resp = requests.get(url)
    # 设置编码格式
    resp.encoding = "utf-8"
    city = re.findall('<span class="name">([\u4e00-\u9fa5]*)</span>', resp.text, re.I)
    weather = re.findall('<span class="weather">([\u4e00-\u9fa5]*)</span>', resp.text, re.I)
    wd = re.findall('<span class="wd">(.*)</span>', resp.text, re.I)
    zs = re.findall('<span class="zs">([\u4e00-\u9fa5]*)</span>', resp.text, re.I)
    # 数据打包
    lst = []
    for a, b, c, d in zip(city, weather, wd, zs):
        lst.append([a, b, c, d])
    content = {
        "v1": lst
    }
    return render(request, "do_tianqi.html", content)

def index(request):
    return render(request, "index.html")
# 获取请求
def get_method(request):
    # 1 获取请求方式
    method = request.method
    # 2 获取URL中传递的参数 /get/method/?n1=qq&m1=ww
    # get请求
    can = request.GET.get("n1")
    # post请求
    content = {
        "v1": method,
        "v2": can,
    }
    return render(request, "index.html", content)
# 简单登录
def login(request):
    if request.method == "GET":
        return render(request, 'login.html')
    uname = request.POST.get('uname')
    pwd = request.POST.get('pwd')
    print(uname, pwd)
    if uname == "admin" and pwd == "123":
        # 登录成功重定向到百度一下
        return redirect("https://www.baidu.com/")
    else:
        # 登录失败，依然停留在登录页面
        return render(request,'login.html',{"error":"用户名或密码错误"})
# 测试数据库
def test_sql(request):
    from app01 import models
    key = request.GET.get('key')
    content = {"info":123}
    # 传递的参数为1  增加
    if key == "1":
        # 1 增加
        models.UserInfo.objects.create(username='萌萌', pwd='123', age=19)
    elif key == "2":
        # 2 删除
        # models.UserInfo.objects.filter(id=1).delete()
        # 并且条件
        models.UserInfo.objects.filter(id=2).delete()
    elif key == "3":
        # 修改 all() 表示这一列所有的都修改
        # models.UserInfo.objects.all().update(age=20)
        # 满足条件id=1的修改
        models.UserInfo.objects.filter(id=3).update(username="嫣然", age=22)
    elif key == "4":
        # 查询的返回的是QuerySet类型(列表，里面都是对象) [obj,obj....]
        res = models.UserInfo.objects.all()
        # res = models.UserInfo.objects.filter(id=2)
        content = {
            "info":res
        }
    # 查询返回对象
    elif key == "5":
        # first() 返回的就是一个对象
        res = models.UserInfo.objects.filter(id=3).first()
        content = {
            "v1":res
        }
    return render(request, "index.html", content)
